"use strict";
cc._RF.push(module, '5c1daszlr1JIJ5qp3WhIIqm', 'SteeringBehaviors');
// cases/entity/SteeringBehaviors.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Path_1 = require("./Path");
// import Wall from "./Wall";
//躲避圆半径
var WanderRad = 1.2;
//在智能体躲避行为中前面圆的距离
var WanderDist = 2;
//躲避最大角度
var WanderJitterPerSec = 80.0;
//路径根据距离阀值
var WaypointSeekDist = 20;
var SummingMethod;
(function (SummingMethod) {
    SummingMethod[SummingMethod["weighted_average"] = 0] = "weighted_average";
    SummingMethod[SummingMethod["prioritized"] = 1] = "prioritized";
    SummingMethod[SummingMethod["dithered"] = 2] = "dithered";
})(SummingMethod || (SummingMethod = {}));
var offset = 0;
var BehaviorType;
(function (BehaviorType) {
    BehaviorType[BehaviorType["none"] = 1 << offset++] = "none";
    BehaviorType[BehaviorType["seek"] = 1 << offset++] = "seek";
    BehaviorType[BehaviorType["flee"] = 1 << offset++] = "flee";
    BehaviorType[BehaviorType["arrive"] = 1 << offset++] = "arrive";
    BehaviorType[BehaviorType["wander"] = 1 << offset++] = "wander";
    BehaviorType[BehaviorType["cohesion"] = 1 << offset++] = "cohesion";
    BehaviorType[BehaviorType["separation"] = 1 << offset++] = "separation";
    BehaviorType[BehaviorType["allignment"] = 1 << offset++] = "allignment";
    BehaviorType[BehaviorType["obstacleAvoidance"] = 1 << offset++] = "obstacleAvoidance";
    BehaviorType[BehaviorType["wallAvoidance"] = 1 << offset++] = "wallAvoidance";
    BehaviorType[BehaviorType["followPath"] = 1 << offset++] = "followPath";
    BehaviorType[BehaviorType["pursuit"] = 1 << offset++] = "pursuit";
    BehaviorType[BehaviorType["evade"] = 1 << offset++] = "evade";
    BehaviorType[BehaviorType["interpose"] = 1 << offset++] = "interpose";
    BehaviorType[BehaviorType["hide"] = 1 << offset++] = "hide";
    BehaviorType[BehaviorType["flock"] = 1 << offset++] = "flock";
    BehaviorType[BehaviorType["offsetPursuit"] = 1 << offset++] = "offsetPursuit";
    BehaviorType[BehaviorType["flow"] = 1 << offset++] = "flow";
})(BehaviorType || (BehaviorType = {}));
var Deceleration;
(function (Deceleration) {
    Deceleration[Deceleration["SLOW"] = 3] = "SLOW";
    Deceleration[Deceleration["NORMAL"] = 2] = "NORMAL";
    Deceleration[Deceleration["FAST"] = 1] = "FAST";
})(Deceleration || (Deceleration = {}));
var tempVec2 = cc.v2();
var tempVec2_2 = cc.v2();
var tempVec2_3 = cc.v2();
var tempVec2_4 = cc.v2();
var tempVec2_5 = cc.v2();
var tempVec2_6 = cc.v2();
var tempMat4 = cc.mat4();
var SteeringBehavior = /** @class */ (function () {
    function SteeringBehavior(agent) {
        this._vehicle = null;
        this._steeringForce = cc.Vec2.ZERO;
        this._targetAgent1 = null;
        this._targetAgent2 = null;
        //权重,使用权重比例计算操控力的才需要
        //////////////////////////////////////////////////////////
        this._weightSeparation = 2 * SteeringBehavior._steeringForceTweaker;
        this._weightCohesion = 0.5 * SteeringBehavior._steeringForceTweaker;
        this._weightAlignment = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightObstacleAvoidance = 10 * SteeringBehavior._steeringForceTweaker;
        this._weightWallAvoidance = 10 * SteeringBehavior._steeringForceTweaker;
        this._weightSeek = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightFlee = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightArrive = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightPursuit = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightOffsetPursuit = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightInterpose = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightHide = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightFollowPath = 0.05 * SteeringBehavior._steeringForceTweaker;
        this._weightEvade = 0.01 * SteeringBehavior._steeringForceTweaker;
        this._weightWander = 1 * SteeringBehavior._steeringForceTweaker;
        this._weightFlow = 3 * SteeringBehavior._steeringForceTweaker;
        //////////////////////////////////////////////////////////
        this._curTarget = cc.Vec2.ZERO;
        this._boxLength = 40; //检测障碍物的探测长度
        this._feelers = [];
        this.debugPen = null;
        this._wallDetectionFeelerLength = 40;
        this._wanderTarget = cc.Vec2.ZERO;
        this._wanderJitter = WanderJitterPerSec;
        this._wanderRadius = WanderRad;
        this._wanderDistance = WanderDist;
        //100半径内可视，用于视野内集群行为力
        this._viewDistance = 50;
        this._wayPointSeekDistSq = WaypointSeekDist * WaypointSeekDist;
        this._offset = cc.Vec2.ZERO;
        this._flags = 0;
        this._deceleration = Deceleration.FAST;
        //标记是否开启空间划分
        this._cellSpaceOn = false;
        //合力计算方式 ，默认优先加权截断
        this._summingMethod = SummingMethod.prioritized;
        //跟随路径
        this._path = null;
        this._seekVec = cc.v2();
        var theta = 2 * Math.random() * Math.PI;
        cc.Vec2.set(this._wanderTarget, this._wanderRadius * Math.cos(theta), this.wanderRadius * Math.sin(theta));
        this._vehicle = agent;
        this._path = new Path_1.default();
        var rect = this._vehicle.world.rect;
        this._path.createRandomPath(Math.round(Math.random() * 7 + 4), rect.xMin, rect.yMin, rect.xMax, rect.yMax, true);
    }
    Object.defineProperty(SteeringBehavior.prototype, "feelers", {
        get: function () {
            return this._feelers;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "wanderTarget", {
        get: function () {
            return this._wanderTarget;
        },
        enumerable: false,
        configurable: true
    });
    SteeringBehavior.prototype.on = function (bt) { return (this._flags & bt) == bt; };
    //按照优先级计算操控力
    SteeringBehavior.prototype.accumulateForce = function (forceToAdd) {
        var mag = this._steeringForce.mag();
        var remain = this._vehicle.maxForce - mag; //剩余多少力
        if (remain <= 0.0)
            return false;
        var toadd = forceToAdd.mag();
        if (toadd < remain) {
            this._steeringForce.addSelf(forceToAdd);
        }
        else {
            this._steeringForce.addSelf(forceToAdd.normalizeSelf().mulSelf(remain));
        }
        return true;
    };
    /* .......................................................

                    BEGIN BEHAVIOR DECLARATIONS

      .......................................................*/
    //靠近目标
    SteeringBehavior.prototype.seek = function (targetPos) {
        this._seekVec.set(targetPos);
        cc.Vec2.subtract(tempVec2, targetPos, this._vehicle.position);
        cc.Vec2.normalize(tempVec2, tempVec2);
        cc.Vec2.multiplyScalar(tempVec2, tempVec2, this._vehicle.maxSpeed);
        cc.Vec2.subtract(tempVec2, tempVec2, this._vehicle.velocity);
        return tempVec2;
    };
    //远离目标
    SteeringBehavior.prototype.flee = function (targetPos) {
        cc.Vec2.subtract(tempVec2, this._vehicle.position, targetPos);
        if (tempVec2.magSqr() > 90000) { //可设定特定距离才远离
            cc.Vec2.set(tempVec2, 0, 0);
            return tempVec2;
        }
        cc.Vec2.normalize(tempVec2, tempVec2);
        cc.Vec2.multiplyScalar(tempVec2, tempVec2, this._vehicle.maxSpeed);
        cc.Vec2.subtract(tempVec2, tempVec2, this._vehicle.velocity);
        return tempVec2;
    };
    //到达某个点
    SteeringBehavior.prototype.arrive = function (targetPos, deceleration) {
        cc.Vec2.subtract(tempVec2, targetPos, this._vehicle.position);
        var dis = tempVec2.mag();
        if (dis > 0.01) {
            var speed = dis / (deceleration * 0.3);
            speed = Math.min(speed, this._vehicle.maxSpeed);
            cc.Vec2.multiplyScalar(tempVec2, tempVec2, speed);
            tempVec2.divSelf(dis);
            cc.Vec2.subtract(tempVec2, tempVec2, this._vehicle.velocity);
            return tempVec2;
        }
        cc.Vec2.set(tempVec2, 0, 0);
        return tempVec2;
    };
    //追逐
    SteeringBehavior.prototype.pursuit = function (agent) {
        if (!agent) {
            tempVec2.set(cc.Vec2.ZERO);
            return tempVec2;
        }
        cc.Vec2.subtract(tempVec2, agent.position, this._vehicle.position);
        var relativeHeading = this._vehicle.heading.dot(agent.heading);
        if (tempVec2.dot(this._vehicle.heading) > 0 && relativeHeading < -0.95) {
            return this.seek(agent.position);
        }
        var lookaheadTime = tempVec2.mag() / (this._vehicle.maxSpeed + agent.velocity.mag());
        cc.Vec2.multiplyScalar(tempVec2_2, agent.velocity, lookaheadTime);
        cc.Vec2.add(tempVec2_2, tempVec2_2, agent.position);
        return this.seek(tempVec2_2);
    };
    SteeringBehavior.prototype.offsetPursuit = function (agent, offset) {
        if (!agent) {
            tempVec2.set(cc.Vec2.ZERO);
            return tempVec2;
        }
        cc.Vec2.set(tempVec2, agent.side.x * offset.x + agent.side.y * offset.y, agent.heading.x * offset.x + agent.heading.y * offset.y);
        cc.Vec2.add(tempVec2, agent.position, tempVec2);
        var dis = cc.Vec2.distance(tempVec2, this._vehicle.position);
        var lookAheadTime = dis / (this._vehicle.maxSpeed + 0);
        cc.Vec2.multiplyScalar(tempVec2_2, agent.velocity, lookAheadTime);
        cc.Vec2.add(tempVec2_2, tempVec2, tempVec2_2);
        return this.arrive(tempVec2_2, this._deceleration);
    };
    //躲避
    SteeringBehavior.prototype.evade = function (agent) {
        cc.Vec2.subtract(tempVec2, agent.position, this._vehicle.position);
        var lensqr = tempVec2.magSqr();
        if (lensqr > 40000) { //可设定特定距离之后不躲避
            cc.Vec2.set(tempVec2, 0, 0);
            return tempVec2;
        }
        else {
            var lookAheadTime = tempVec2.mag() / (this._vehicle.maxSpeed + agent.velocity.mag());
            cc.Vec2.multiplyScalar(tempVec2, agent.velocity, lookAheadTime);
            cc.Vec2.add(tempVec2_2, tempVec2, agent.position);
            return this.flee(tempVec2_2);
        }
    };
    SteeringBehavior.prototype.randomClamped = function () {
        return Math.random() - Math.random();
    };
    //徘徊
    SteeringBehavior.prototype.wander = function () {
        var jitterTimeScale = this._wanderJitter * this._vehicle.timeElapse();
        cc.Vec2.set(tempVec2_2, this.randomClamped() * jitterTimeScale, this.randomClamped() * jitterTimeScale);
        cc.Vec2.add(this._wanderTarget, this._wanderTarget, tempVec2_2);
        this._wanderTarget.normalizeSelf();
        cc.Vec2.multiplyScalar(this._wanderTarget, this._wanderTarget, this._wanderRadius);
        cc.Vec2.set(tempVec2_2, 0, this._wanderDistance);
        cc.Vec2.add(tempVec2, this._wanderTarget, tempVec2_2);
        this._vehicle.node.getLocalMatrix(tempMat4);
        cc.Vec2.transformMat4(tempVec2, tempVec2, tempMat4);
        cc.Vec2.subtract(tempVec2, tempVec2, this._vehicle.position);
        return tempVec2;
    };
    //躲避障碍物
    SteeringBehavior.prototype.obstacleAvoidance = function (obstacles) {
        this._boxLength = 40 + (this._vehicle.velocity.mag() / this._vehicle.maxSpeed) * 40;
        var distToClose = Number.MAX_SAFE_INTEGER;
        var closeIndex = -1;
        cc.Vec2.set(tempVec2_3, 0, 0);
        this._vehicle.node.getLocalMatrix(tempMat4);
        cc.Mat4.invert(tempMat4, tempMat4);
        for (var i = 0, len = obstacles.length; i < len; i++) {
            var obstacle = obstacles[i];
            cc.Vec2.set(tempVec2_2, obstacle.node.x, obstacle.node.y);
            cc.Vec2.transformMat4(tempVec2_2, tempVec2_2, tempMat4);
            if (tempVec2_2.y >= 0 && tempVec2_2.y <= this._boxLength + obstacle.radius) {
                //圆方程 (x-a)^2+(y-b)^2=r^2  a,b圆心坐标 r半径
                //y = b+-sqrt(r^2-a^2)
                var expandeRadius = obstacle.radius + this._vehicle.radius;
                if (Math.abs(tempVec2_2.x) < expandeRadius) {
                    var sqrtpart = Math.sqrt(expandeRadius * expandeRadius - tempVec2_2.x * tempVec2_2.x);
                    var ip = tempVec2_2.y - sqrtpart;
                    if (ip <= 0) {
                        ip = tempVec2_2.y + sqrtpart;
                    }
                    if (ip < distToClose) {
                        distToClose = ip;
                        closeIndex = i;
                        cc.Vec2.set(tempVec2_3, tempVec2_2.x, tempVec2_2.y);
                    }
                }
            }
        }
        if (closeIndex >= 0) {
            var closeobstacle = obstacles[closeIndex];
            var multiplier = 1.0 + (this._boxLength - tempVec2_3.y) / this._boxLength;
            tempVec2_2.x = (closeobstacle.radius - tempVec2_3.x) * multiplier;
            tempVec2_2.y = (closeobstacle.radius - tempVec2_3.y) * 0.2;
            this._vehicle.node.getLocalMatrix(tempMat4);
            cc.Vec2.transformMat4(tempVec2, tempVec2_2, tempMat4);
            return tempVec2;
        }
        else {
            tempVec2_2.set(cc.Vec2.ZERO);
            return tempVec2_2;
        }
    };
    SteeringBehavior.prototype._createFeelers = function () {
        //中间触角
        cc.Vec2.multiplyScalar(tempVec2, this._vehicle.heading, 1.2 * this._vehicle.radius);
        cc.Vec2.add(tempVec2, this._vehicle.position, tempVec2);
        if (this._feelers.length <= 0) {
            this._feelers.push(cc.v2(tempVec2));
        }
        else {
            this._feelers[0].set(tempVec2);
        }
        //左边触角
        this._vehicle.heading.rotate(cc.misc.degreesToRadians(60), tempVec2);
        cc.Vec2.multiplyScalar(tempVec2, tempVec2, this._vehicle.radius);
        cc.Vec2.add(tempVec2, tempVec2, this._vehicle.position);
        if (this._feelers.length <= 1) {
            this._feelers.push(cc.v2(tempVec2));
        }
        else {
            this._feelers[1].set(tempVec2);
        }
        //右边触角
        this._vehicle.heading.rotate(cc.misc.degreesToRadians(-60), tempVec2);
        cc.Vec2.multiplyScalar(tempVec2, tempVec2, this._vehicle.radius);
        cc.Vec2.add(tempVec2, tempVec2, this._vehicle.position);
        if (this._feelers.length <= 2) {
            this._feelers.push(cc.v2(tempVec2));
        }
        else {
            this._feelers[2].set(tempVec2);
        }
    };
    //躲避墙
    // private wallAvoidance(walls: Wall[]): cc.Vec2 {
    //     this._createFeelers();
    //     let closeWall = -1;
    //     let distClose = 1 << 30;
    //     for (let i = 0, len = this._feelers.length; i < len; i++) {
    //         for (let j = 0, jlen = walls.length; j < jlen; j++) {
    //             let info = this._lineIntersection2D(this._vehicle.position, this._feelers[i], walls[j].from, walls[j].to, tempVec2_4);
    //             if (info[0]) {
    //                 if (info[1] < distClose) {
    //                     distClose = info[1];
    //                     closeWall = j;
    //                     tempVec2_5.set(tempVec2_4);
    //                 }
    //             }
    //         }
    //         if (closeWall >= 0) {
    //             cc.Vec2.subtract(tempVec2_5, this._feelers[i], tempVec2_5);
    //             cc.Vec2.multiplyScalar(tempVec2, walls[closeWall].normal, tempVec2_5.mag() * 2);
    //         }
    //     }
    //     if (closeWall == -1) {
    //         tempVec2.set(cc.Vec2.ZERO);
    //     }
    //     return tempVec2;
    // }
    //路径跟随
    SteeringBehavior.prototype.followPath = function () {
        var waypoint = this._path.getCurPoint();
        if (!waypoint)
            return tempVec2.set(cc.Vec2.ZERO);
        if (cc.Vec2.squaredDistance(waypoint, this._vehicle.position) < this._wayPointSeekDistSq) {
            this._path.setUpNextPoint();
        }
        if (!this._path.isFinish()) {
            return this.seek(waypoint);
        }
        else {
            return this.arrive(waypoint, Deceleration.NORMAL);
        }
    };
    SteeringBehavior.prototype.interpose = function (vehicleA, vehicleB) {
        cc.Vec2.add(tempVec2, vehicleA.position, vehicleB.position);
        cc.Vec2.multiplyScalar(tempVec2, tempVec2, 0.5);
        var timeToReachMidPoint = cc.Vec2.distance(this._vehicle.position, tempVec2) / this._vehicle.maxSpeed;
        cc.Vec2.multiplyScalar(tempVec2, vehicleA.velocity, timeToReachMidPoint);
        cc.Vec2.add(tempVec2, tempVec2, vehicleA.position);
        cc.Vec2.multiplyScalar(tempVec2_2, vehicleB.velocity, timeToReachMidPoint);
        cc.Vec2.add(tempVec2_2, tempVec2_2, vehicleB.position);
        cc.Vec2.add(tempVec2, tempVec2, tempVec2_2);
        cc.Vec2.multiplyScalar(tempVec2_2, tempVec2, 0.5);
        return this.arrive(tempVec2_2, Deceleration.FAST);
    };
    SteeringBehavior.prototype.hide = function (hunter, obstacles) {
        var distToClosest = 999999999;
        var closeIndex = -1;
        cc.Vec2.set(tempVec2_3, 0, 0);
        for (var i = 0, len = obstacles.length; i < len; i++) {
            var obstacle = obstacles[i];
            var vec = this._getHidingPosition(obstacle.position, obstacle.radius, hunter.position);
            var dist = cc.Vec2.squaredDistance(vec, this._vehicle.position);
            if (dist < distToClosest) {
                distToClosest = dist;
                cc.Vec2.set(tempVec2_3, vec.x, vec.y);
                closeIndex = i;
            }
        }
        if (closeIndex == -1) {
            return this.evade(hunter);
        }
        else {
            return this.arrive(tempVec2_3, Deceleration.FAST);
        }
    };
    SteeringBehavior.prototype._getHidingPosition = function (posOb, radiusOb, posHunter) {
        var disFromBoundary = 30;
        var disAway = radiusOb + disFromBoundary;
        cc.Vec2.subtract(tempVec2, posOb, posHunter);
        tempVec2.normalizeSelf();
        cc.Vec2.multiplyScalar(tempVec2, tempVec2, disAway);
        cc.Vec2.add(tempVec2_2, tempVec2, posHunter);
        return tempVec2_2;
    };
    SteeringBehavior.prototype.flow = function () {
        var pos2 = this._vehicle.world.getColAndRowByPosition(this._vehicle.position.x, this._vehicle.position.y);
        if (this._vehicle.world.isVaildGrid(pos2[0], pos2[1])) {
            //TODO
            var gridcell = this._vehicle.world.gridCells[pos2[0]][pos2[1]];
            var offsetx = 0, offsety = 0, dirx = 0, diry = 0;
            var pos = this._vehicle.world.getPixelCenterInTilePos(tempVec2_3.x, tempVec2_3.y);
            if (Math.abs(gridcell.flowDir.x) <= 0.02) {
                diry = gridcell.flowDir.y > 0 ? 1 : -1;
                dirx = pos[0] > this._vehicle.position.x ? -1 : 1;
                if (!this._vehicle.world.isVaildGrid(tempVec2_3.x + dirx, tempVec2_3.y + diry)) {
                    offsetx = -0.25 * dirx;
                }
            }
            else if (Math.abs(gridcell.flowDir.y) <= 0.02) {
                dirx = gridcell.flowDir.x > 0 ? 1 : -1;
                diry = pos[1] > this._vehicle.position.y ? -1 : 1;
                if (!this._vehicle.world.isVaildGrid(tempVec2_3.x + dirx, tempVec2_3.y + diry)) {
                    offsety = 0.25 * diry;
                }
            }
            cc.Vec2.set(tempVec2, gridcell.flowDir.x + offsetx, -(gridcell.flowDir.y + offsety));
        }
        else {
            tempVec2.set(cc.Vec2.ZERO);
        }
        return tempVec2;
    };
    // -- Group Behaviors -- //
    SteeringBehavior.prototype.cohesion = function (agents) {
        cc.Vec2.set(tempVec2_6, 0, 0);
        cc.Vec2.set(tempVec2_5, 0, 0);
        var neighborcount = 0;
        for (var i = 0, len = agents.length; i < len; i++) {
            var agent = agents[i];
            if (agent != this._vehicle && agent != this._targetAgent1 && agent.isTag) {
                cc.Vec2.add(tempVec2_6, tempVec2_6, agent.position);
                neighborcount++;
            }
        }
        if (neighborcount > 0) {
            tempVec2_6.divSelf(neighborcount);
            var tempv2 = this.seek(tempVec2_6);
            cc.Vec2.set(tempVec2_5, tempv2.x, tempv2.y);
            tempVec2_5.normalizeSelf();
            return tempVec2_5;
        }
        return cc.Vec2.ZERO;
    };
    SteeringBehavior.prototype.separation = function (agents) {
        cc.Vec2.set(tempVec2, 0, 0);
        for (var i = 0, len = agents.length; i < len; i++) {
            if (agents[i].ID != this._vehicle.ID && agents[i].isTag && agents[i] != this._targetAgent1) {
                cc.Vec2.subtract(tempVec2_2, this._vehicle.position, agents[i].position);
                var len_1 = tempVec2_2.mag();
                cc.Vec2.normalize(tempVec2_2, tempVec2_2);
                tempVec2_2.divSelf(len_1);
                cc.Vec2.add(tempVec2, tempVec2_2, tempVec2);
            }
        }
        return tempVec2;
    };
    SteeringBehavior.prototype.alignment = function (agents) {
        cc.Vec2.set(tempVec2_6, 0, 0);
        cc.Vec2.set(tempVec2_5, 0, 0);
        var neighborcount = 0;
        for (var i = 0, len = agents.length; i < len; i++) {
            if (agents[i].ID != this._vehicle.ID && agents[i].isTag && agents[i] != this._targetAgent1) {
                cc.Vec2.add(tempVec2_6, tempVec2_6, agents[i].heading);
                neighborcount++;
            }
        }
        if (neighborcount > 0) {
            tempVec2_6.divSelf(neighborcount);
            cc.Vec2.subtract(tempVec2_5, tempVec2_6, this._vehicle.heading);
        }
        return tempVec2_5;
    };
    SteeringBehavior.prototype.cohesionPlus = function (agents) { return cc.Vec2.ZERO; };
    SteeringBehavior.prototype.separationPlus = function (agents) { return cc.Vec2.ZERO; };
    SteeringBehavior.prototype.alignmentPlus = function (agents) { return cc.Vec2.ZERO; };
    /* .......................................................

                   END BEHAVIOR DECLARATIONS

  .......................................................*/
    SteeringBehavior.prototype.calculateWeightedSum = function () { return cc.Vec2.ZERO; };
    SteeringBehavior.prototype.calculatePrioritized = function () {
        var force;
        // if (this.on(BehaviorType.wallAvoidance)) {
        //     force = this.wallAvoidance(this._vehicle.world.walls);
        //     cc.Vec2.multiplyScalar(force, force, this._weightWallAvoidance);
        //     if (!this.accumulateForce(force)) return this._steeringForce;
        // }
        if (this.on(BehaviorType.obstacleAvoidance)) {
            force = this.obstacleAvoidance(this._vehicle.world.obstacles);
            cc.Vec2.multiplyScalar(force, force, this._weightObstacleAvoidance);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.flow)) {
            force = this.flow();
            cc.Vec2.multiplyScalar(force, force, this._weightFlow);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.evade)) { //躲避
            force = this.evade(this._targetAgent1);
            cc.Vec2.multiplyScalar(force, force, this._weightEvade);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.flee)) { //远离
            force = this.flee(this._curTarget);
            cc.Vec2.multiplyScalar(force, force, this._weightFlee);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        //组合行为
        if (!this.isSpacePartitioningOn()) {
            if (this.on(BehaviorType.separation)) {
                force = this.separation(this._vehicle.world.agents);
                cc.Vec2.multiplyScalar(force, force, this._weightSeparation);
                if (!this.accumulateForce(force))
                    return this._steeringForce;
            }
            if (this.on(BehaviorType.allignment)) {
                force = this.alignment(this._vehicle.world.agents);
                cc.Vec2.multiplyScalar(force, force, this._weightAlignment);
                if (!this.accumulateForce(force))
                    return this._steeringForce;
            }
            if (this.on(BehaviorType.cohesion)) {
                force = this.cohesion(this._vehicle.world.agents);
                cc.Vec2.multiplyScalar(force, force, this._weightCohesion);
                if (!this.accumulateForce(force))
                    return this._steeringForce;
            }
        }
        else {
            if (this.on(BehaviorType.separation)) {
                force = this.separationPlus(this._vehicle.world.agents);
                cc.Vec2.multiplyScalar(force, force, this._weightSeparation);
                if (!this.accumulateForce(force))
                    return this._steeringForce;
            }
            if (this.on(BehaviorType.allignment)) {
                force = this.alignmentPlus(this._vehicle.world.agents);
                cc.Vec2.multiplyScalar(force, force, this._weightAlignment);
                if (!this.accumulateForce(force))
                    return this._steeringForce;
            }
            if (this.on(BehaviorType.cohesion)) {
                force = this.cohesionPlus(this._vehicle.world.agents);
                cc.Vec2.multiplyScalar(force, force, this._weightCohesion);
                if (!this.accumulateForce(force))
                    return this._steeringForce;
            }
        }
        if (this.on(BehaviorType.seek)) {
            force = this.seek(this._curTarget);
            cc.Vec2.multiplyScalar(force, force, this._weightSeek);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.arrive)) {
            force = this.arrive(this._curTarget, this._deceleration);
            cc.Vec2.multiplyScalar(force, force, this._weightArrive);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.wander)) {
            force = this.wander();
            cc.Vec2.multiplyScalar(force, force, this._weightWander);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.pursuit)) {
            force = this.pursuit(this._targetAgent1);
            cc.Vec2.multiplyScalar(force, force, this._weightPursuit);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.offsetPursuit)) {
            force = this.offsetPursuit(this._targetAgent1, this._offset);
            cc.Vec2.multiplyScalar(force, force, this._weightOffsetPursuit);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.interpose)) {
            force = this.interpose(this._targetAgent1, this._targetAgent2);
            cc.Vec2.multiplyScalar(force, force, this._weightInterpose);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.hide)) {
            force = this.hide(this._targetAgent1, this._vehicle.world.obstacles);
            cc.Vec2.multiplyScalar(force, force, this._weightHide);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        if (this.on(BehaviorType.followPath)) {
            force = this.followPath();
            cc.Vec2.multiplyScalar(force, force, this._weightFollowPath);
            if (!this.accumulateForce(force))
                return this._steeringForce;
        }
        return this._steeringForce;
    };
    SteeringBehavior.prototype.calculateDithered = function () { return cc.Vec2.ZERO; };
    SteeringBehavior.prototype.getHidingPosition = function (posOb, radiusOb, posHunter) { return cc.Vec2.ZERO; };
    SteeringBehavior.prototype.calculate = function () {
        this._steeringForce.set(cc.Vec2.ZERO);
        if (!this.isSpacePartitioningOn()) {
            if (this.on(BehaviorType.separation) || this.on(BehaviorType.allignment) || this.on(BehaviorType.cohesion)) {
                this._vehicle.world.tagVehiclesWithinViewRange(this._vehicle, this._viewDistance);
            }
        }
        else {
            if (this.on(BehaviorType.separation) || this.on(BehaviorType.allignment) || this.on(BehaviorType.cohesion)) {
                //TODO: 空间划分标记，快速划分，测试样例暂不提供
            }
        }
        if (this._summingMethod == SummingMethod.prioritized) {
            this.calculatePrioritized();
        }
        else if (this._summingMethod == SummingMethod.weighted_average) {
            this.calculateWeightedSum();
        }
        else if (this._summingMethod == SummingMethod.dithered) {
            this.calculateDithered();
        }
        this.debugRender();
        return this._steeringForce;
    };
    SteeringBehavior.prototype.setTarget = function (t) { this._curTarget = t; };
    SteeringBehavior.prototype.setTargetAgent1 = function (agent) { this._targetAgent1 = agent; };
    SteeringBehavior.prototype.setTargetAgent2 = function (agent) { this._targetAgent2 = agent; };
    Object.defineProperty(SteeringBehavior.prototype, "targetPos", {
        get: function () { return this._curTarget; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "targetAgent1", {
        get: function () { return this._targetAgent1; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "targetAgent2", {
        get: function () { return this._targetAgent2; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "offset", {
        get: function () { return this._offset; },
        set: function (offset) { this._offset = offset; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "force", {
        get: function () { return this._steeringForce; },
        enumerable: false,
        configurable: true
    });
    SteeringBehavior.prototype.toggleSpacePartitioningOnOff = function () { this._cellSpaceOn = !this._cellSpaceOn; };
    SteeringBehavior.prototype.isSpacePartitioningOn = function () { return this._cellSpaceOn; };
    SteeringBehavior.prototype.setSummingMethod = function (sm) { this._summingMethod = sm; };
    Object.defineProperty(SteeringBehavior.prototype, "boxLength", {
        get: function () { return this._boxLength; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "wanderJitter", {
        get: function () { return this._wanderJitter; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "wanderRadius", {
        get: function () { return this._wanderRadius; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "separationWeight", {
        get: function () { return this._weightSeparation; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "alignmentWeight", {
        get: function () { return this._weightAlignment; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(SteeringBehavior.prototype, "cohesionWeight", {
        get: function () { return this._weightCohesion; },
        enumerable: false,
        configurable: true
    });
    SteeringBehavior.prototype.fleeOn = function () { this._flags |= BehaviorType.flee; };
    SteeringBehavior.prototype.seekOn = function () { this._flags |= BehaviorType.seek; };
    SteeringBehavior.prototype.arriveOn = function () { this._flags |= BehaviorType.arrive; };
    SteeringBehavior.prototype.wanderOn = function () { this._flags |= BehaviorType.wander; };
    SteeringBehavior.prototype.pursuitOn = function (v) { this._flags |= BehaviorType.pursuit; this._targetAgent1 = v; };
    SteeringBehavior.prototype.evadeOn = function (v) { this._flags |= BehaviorType.evade; this._targetAgent1 = v; };
    SteeringBehavior.prototype.cohesionOn = function () { this._flags |= BehaviorType.cohesion; };
    SteeringBehavior.prototype.separationOn = function () { this._flags |= BehaviorType.separation; };
    SteeringBehavior.prototype.alignmentOn = function () { this._flags |= BehaviorType.allignment; };
    SteeringBehavior.prototype.obstacleAvoidanceOn = function () { this._flags |= BehaviorType.obstacleAvoidance; };
    SteeringBehavior.prototype.wallAvoidanceOn = function () { this._flags |= BehaviorType.wallAvoidance; };
    SteeringBehavior.prototype.followPathOn = function () { this._flags |= BehaviorType.followPath; };
    SteeringBehavior.prototype.interposeOn = function (v1, v2) { this._flags |= BehaviorType.interpose; this._targetAgent1 = v1; this._targetAgent2 = v2; };
    SteeringBehavior.prototype.hideOn = function (v) { this._flags |= BehaviorType.hide; this._targetAgent1 = v; };
    SteeringBehavior.prototype.offsetPursuitOn = function (v1, offset) { this._flags |= BehaviorType.offsetPursuit; this._offset = offset; this._targetAgent1 = v1; };
    SteeringBehavior.prototype.flockingOn = function () { this.cohesionOn(); this.alignmentOn(); this.separationOn(); };
    SteeringBehavior.prototype.flowOn = function () { this._flags |= BehaviorType.flow; };
    SteeringBehavior.prototype.fleeOff = function () { if (this.on(BehaviorType.flee))
        this._flags ^= BehaviorType.flee; };
    SteeringBehavior.prototype.seekOff = function () { if (this.on(BehaviorType.seek))
        this._flags ^= BehaviorType.seek; };
    SteeringBehavior.prototype.arriveOff = function () { if (this.on(BehaviorType.arrive))
        this._flags ^= BehaviorType.arrive; };
    SteeringBehavior.prototype.wanderOff = function () { if (this.on(BehaviorType.wander))
        this._flags ^= BehaviorType.wander; };
    SteeringBehavior.prototype.pursuitOff = function () { if (this.on(BehaviorType.pursuit))
        this._flags ^= BehaviorType.pursuit; };
    SteeringBehavior.prototype.evadeOff = function () { if (this.on(BehaviorType.evade))
        this._flags ^= BehaviorType.evade; };
    SteeringBehavior.prototype.cohesionOff = function () { if (this.on(BehaviorType.cohesion))
        this._flags ^= BehaviorType.cohesion; };
    SteeringBehavior.prototype.separationOff = function () { if (this.on(BehaviorType.separation))
        this._flags ^= BehaviorType.separation; };
    SteeringBehavior.prototype.alignmentOff = function () { if (this.on(BehaviorType.allignment))
        this._flags ^= BehaviorType.allignment; };
    SteeringBehavior.prototype.obstacleAvoidanceOff = function () { if (this.on(BehaviorType.obstacleAvoidance))
        this._flags ^= BehaviorType.obstacleAvoidance; };
    SteeringBehavior.prototype.wallAvoidanceOff = function () { if (this.on(BehaviorType.wallAvoidance))
        this._flags ^= BehaviorType.wallAvoidance; };
    SteeringBehavior.prototype.followPathOff = function () { if (this.on(BehaviorType.followPath))
        this._flags ^= BehaviorType.followPath; };
    SteeringBehavior.prototype.interposeOff = function () { if (this.on(BehaviorType.interpose))
        this._flags ^= BehaviorType.interpose; };
    SteeringBehavior.prototype.hideOff = function () { if (this.on(BehaviorType.hide))
        this._flags ^= BehaviorType.hide; };
    SteeringBehavior.prototype.offsetPursuitOff = function () { if (this.on(BehaviorType.offsetPursuit))
        this._flags ^= BehaviorType.offsetPursuit; };
    SteeringBehavior.prototype.flockingOff = function () { this.cohesionOff(); this.alignmentOff(); this.separationOff(); };
    SteeringBehavior.prototype.flowOff = function () { if (this.on(BehaviorType.flow))
        this._flags ^= BehaviorType.flow; };
    SteeringBehavior.prototype.isFleeOn = function () { return this.on(BehaviorType.flee); };
    SteeringBehavior.prototype.isSeekOn = function () { return this.on(BehaviorType.seek); };
    SteeringBehavior.prototype.isArriveOn = function () { return this.on(BehaviorType.arrive); };
    SteeringBehavior.prototype.isWanderOn = function () { return this.on(BehaviorType.wander); };
    SteeringBehavior.prototype.isPursuitOn = function () { return this.on(BehaviorType.pursuit); };
    SteeringBehavior.prototype.isEvadeOn = function () { return this.on(BehaviorType.evade); };
    SteeringBehavior.prototype.isCohesionOn = function () { return this.on(BehaviorType.cohesion); };
    SteeringBehavior.prototype.isSeparationOn = function () { return this.on(BehaviorType.separation); };
    SteeringBehavior.prototype.isAlignmentOn = function () { return this.on(BehaviorType.allignment); };
    SteeringBehavior.prototype.isObstacleAvoidanceOn = function () { return this.on(BehaviorType.obstacleAvoidance); };
    SteeringBehavior.prototype.isWallAvoidanceOn = function () { return this.on(BehaviorType.wallAvoidance); };
    SteeringBehavior.prototype.isFollowPathOn = function () { return this.on(BehaviorType.followPath); };
    SteeringBehavior.prototype.isInterposeOn = function () { return this.on(BehaviorType.interpose); };
    SteeringBehavior.prototype.isHideOn = function () { return this.on(BehaviorType.hide); };
    SteeringBehavior.prototype.isOffsetPursuitOn = function () { return this.on(BehaviorType.offsetPursuit); };
    SteeringBehavior.prototype.isFlowOn = function () { return this.on(BehaviorType.flow); };
    SteeringBehavior.prototype.resetFlag = function () {
        this._flags = 0;
    };
    SteeringBehavior.prototype._lineIntersection2D = function (A, B, C, D, point) {
        var rTop = (A.y - C.y) * (D.x - C.x) - (A.x - C.x) * (D.y - C.y);
        var rBot = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x);
        var sTop = (A.y - C.y) * (B.x - A.x) - (A.x - C.x) * (B.y - A.y);
        var sBot = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x);
        if ((rBot == 0) || (sBot == 0)) {
            //lines are parallel
            return [false, 0];
        }
        var r = rTop / rBot;
        var s = sTop / sBot;
        if ((r > 0) && (r < 1) && (s > 0) && (s < 1)) {
            point.x = (B.x - A.x) * r + A.x;
            point.y = (B.y - A.y) * r + A.y;
            return [true, cc.Vec2.distance(A, B) * r];
        }
        else {
            return [false, 0];
        }
    };
    SteeringBehavior.prototype.debugRender = function () {
        if (this._vehicle.world.isDebug && this.debugPen) {
            this.debugPen.strokeColor = cc.Color.WHITE;
            if (this.isFollowPathOn()) {
                var path = this._path.getPath();
                for (var i = 0, len = path.length; i < len; i++) {
                    var pos = path[i];
                    if (i == 0) {
                        this.debugPen.moveTo(pos.x, pos.y);
                    }
                    else {
                        this.debugPen.lineTo(pos.x, pos.y);
                    }
                }
                this.debugPen.stroke();
            }
            if (this.isWanderOn()) {
                this._vehicle.node.getLocalMatrix(tempMat4);
                cc.Vec2.set(tempVec2_2, 0, this._wanderDistance * this._vehicle.radius);
                cc.Vec2.transformMat4(tempVec2_2, tempVec2_2, tempMat4);
                this.debugPen.strokeColor = cc.Color.GREEN;
                this.debugPen.circle(tempVec2_2.x, tempVec2_2.y, this._wanderRadius * this._vehicle.radius);
                this.debugPen.stroke();
                this.debugPen.strokeColor = cc.Color.RED;
                cc.Vec2.set(tempVec2_2, 0, this._wanderDistance);
                cc.Vec2.add(tempVec2_2, this._wanderTarget, tempVec2_2);
                cc.Vec2.multiplyScalar(tempVec2_2, tempVec2_2, this._vehicle.radius);
                cc.Vec2.transformMat4(tempVec2_2, tempVec2_2, tempMat4);
                this.debugPen.circle(tempVec2_2.x, tempVec2_2.y, 3);
                this.debugPen.stroke();
            }
            if (this.isPursuitOn() || this.isSeekOn()) {
                this.debugPen.fillColor = cc.Color.RED;
                this.debugPen.circle(this._seekVec.x, this._seekVec.y, 10);
                this.debugPen.fill();
            }
        }
    };
    SteeringBehavior._steeringForceTweaker = 200;
    return SteeringBehavior;
}());
exports.default = SteeringBehavior;

cc._RF.pop();